/*
 * PlayerMovementController.hpp
 *
 *  Created on: Jun 2, 2009
 *      Author: asantos
 */

#ifndef PLAYERMOVEMENTCONTROLLER_HPP_
#define PLAYERMOVEMENTCONTROLLER_HPP_

class PlayerMovementController{
public:
	typedef int int_tp;
private:
	SignalBroker& signalbroker;
	boost::signals::connection getselectedscenegraphconnection;
	bool initialized;
	const SceneNode* player1node;
	const SceneNode* player2node;
public:
	PlayerMovementController(SignalBroker& signalbroker):
		signalbroker(signalbroker),
		initialized(false){
		Signal<Simulation1InputView::PlayerMovementHandler>& player1moveup = signalbroker.CreateOrGetExistingSignal<Simulation1InputView::PlayerMovementHandler>("/input/player1/moveup");
		player1moveup.connect(boost::bind(&PlayerMovementController::MovePlayer1NodeUp, this, _1, _2));

		Signal<Simulation1InputView::PlayerMovementHandler>& player1movedown = signalbroker.CreateOrGetExistingSignal<Simulation1InputView::PlayerMovementHandler>("/input/player1/movedown");
		player1movedown.connect(boost::bind(&PlayerMovementController::MovePlayer1NodeDown, this, _1, _2));

		Signal<Simulation1InputView::PlayerMovementHandler>& player2moveup = signalbroker.CreateOrGetExistingSignal<Simulation1InputView::PlayerMovementHandler>("/input/player2/moveup");
		player2moveup.connect(boost::bind(&PlayerMovementController::MovePlayer2NodeUp, this, _1, _2));

		Signal<Simulation1InputView::PlayerMovementHandler>& player2movedown = signalbroker.CreateOrGetExistingSignal<Simulation1InputView::PlayerMovementHandler>("/input/player2/movedown");
		player2movedown.connect(boost::bind(&PlayerMovementController::MovePlayer2NodeDown, this, _1, _2));

		Signal<SceneGraphController::GetSelectedSceneGraphHandler>& getselectedscenegraph = signalbroker.CreateOrGetExistingSignal<SceneGraphController::GetSelectedSceneGraphHandler>("/scenegraphcontroller/getselectedscenegraph");
		getselectedscenegraphconnection = getselectedscenegraph.connect(boost::bind(&PlayerMovementController::GetSelectedSceneGraph, this, _1));
	}
protected:
	void GetSelectedSceneGraph(SceneGraph& scenegraph){

		//TODO: get player1 scene node to be manipulated
		try{
			player1node = scenegraph.GetRoot().GetChildNodePtrByName("paddle0");
			player2node = scenegraph.GetRoot().GetChildNodePtrByName("paddle1");

			getselectedscenegraphconnection.disconnect();
			initialized = true;
		}catch(...){

		}
	}

	void MovePlayer1NodeUp(long t, long dt){
		if(initialized){
			if(player1node->HasSceneNodeProperty("position")){
				float dtf = dt/1000.0f;

				PositionProperty& position = player1node->GetSceneNodeProperty<PositionProperty>("position");

				Matrix4& m = position.GetPosition();
				m(1,3)-=100.0f * dtf;
			}
		}
	}
	void MovePlayer1NodeDown(long t, long dt){
		if(initialized){
			if(player1node->HasSceneNodeProperty("position")){
				float dtf = dt/1000.0f;

				PositionProperty& position = player1node->GetSceneNodeProperty<PositionProperty>("position");

				Matrix4& m = position.GetPosition();
				m(1,3)+=100.0f * dtf;
			}
		}
	}
	void MovePlayer2NodeUp(long t, long dt){
		if(initialized){
			if(player1node->HasSceneNodeProperty("position")){
				float dtf = dt/1000.0f;

				PositionProperty& position = player2node->GetSceneNodeProperty<PositionProperty>("position");

				Matrix4& m = position.GetPosition();
				m(1,3)-=100.0f * dtf;
			}
		}
	}
	void MovePlayer2NodeDown(long t, long dt){
		if(initialized){
			if(player2node->HasSceneNodeProperty("position")){
				float dtf = dt/1000.0f;

				PositionProperty& position = player2node->GetSceneNodeProperty<PositionProperty>("position");

				Matrix4& m = position.GetPosition();
				m(1,3)+=100.0f * dtf;
			}
		}
	}
};

#endif /* PLAYERMOVEMENTCONTROLLER_HPP_ */
